package com.xyht.sca_s.student_manage_system.modules.common.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.common.entity.SmsImportTask;
import com.xyht.sca_s.student_manage_system.modules.common.entity.SmsImportTaskDetail;
import com.xyht.sca_s.student_manage_system.modules.common.mapper.SmsImportTaskDetailMapper;
import com.xyht.sca_s.student_manage_system.modules.common.mapper.SmsImportTaskMapper;
import com.xyht.sca_s.student_manage_system.modules.common.service.SmsImportTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.modules.user.entity.SmsUser;
import com.xyht.sca_s.student_manage_system.modules.user.mapper.SmsUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 批量导入任务表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2022-11-29
 */
@Service
public class SmsImportTaskServiceImpl extends ServiceImpl<SmsImportTaskMapper, SmsImportTask> implements SmsImportTaskService {
    @Autowired
    private SmsImportTaskMapper smsImportTaskMapper;
    @Autowired
    private SmsImportTaskDetailMapper smsImportTaskDetailMapper;
    @Autowired
    private SmsUserMapper smsUserMapper;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @Override
    public ResponseResult getImportTaskList(Integer task_type,HttpServletRequest request,Integer pageSize,Integer pageNum) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        if (isNullOrEmpty(user_id)){
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        SmsUser smsUser = smsUserMapper.selectById(user_id);
        if (smsUser == null){
            return CommonResult.failed(CommonCodeEnum.ACCOUNT_ERR);
        }
        QueryWrapper<SmsImportTask> queryWrapper = new QueryWrapper<>();
        if (!isNullOrEmpty(task_type)){
            queryWrapper.lambda().eq(SmsImportTask::getTaskType,task_type);
        }
        queryWrapper.lambda().eq(SmsImportTask::getUserId,user_id).orderByDesc(SmsImportTask::getCreateTime);
        Page<SmsImportTask> page = new Page<>(pageNum,pageSize);
        smsImportTaskMapper.selectPage(page,queryWrapper);

        List<SmsImportTask> list = page.getRecords();
        int total = (int)page.getTotal();
        return CommonResult.success(list,total);
    }

    @Override
    public ResponseResult getImportTaskDetail(String task_id) {
       if (isNullOrEmpty(task_id)){
           return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
       }
        SmsImportTask smsImportTask = smsImportTaskMapper.selectById(task_id);
       if (smsImportTask == null ){
           return CommonResult.failed(CommonCodeEnum.TASK_NOT_EXIST);
       }

       QueryWrapper<SmsImportTaskDetail> queryWrapper = new QueryWrapper<>();
       queryWrapper.lambda().eq(SmsImportTaskDetail::getTaskId,task_id);
       List<SmsImportTaskDetail> list = smsImportTaskDetailMapper.selectList(queryWrapper);
       return CommonResult.success(list);
    }
}
